package edu.cmu.ece.ds.droidchat.controller;

import java.util.ArrayList;

import android.content.Context;
import android.preference.PreferenceManager;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import edu.cmu.ece.ds.droidchat.R;
import edu.cmu.ece.ds.droidchat.model.ChatMessage;
import edu.cmu.ece.ds.droidchat.model.MessageBackgroundColor;
import edu.cmu.ece.ds.droidchat.model.StringConstants;
import edu.cmu.ece.ds.droidchat.model.UserInfo;
import edu.cmu.ece.ds.droidchat.preference.ColorPreference;
import edu.cmu.ece.ds.droidchat.util.Toolbox;
import edu.cmu.ece.ds.droidchat.widget.ActionItem;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList.OnListClickListener;

public class ChatSessionMessageBaseAdapter extends BaseAdapter {
    private static final String TAG = "ChatSessionMessageBaseAdapter";
    private ArrayList<ChatMessage> messageArray;
    private LayoutInflater mInflater;
    private int bgColorResId = 0;

    public ChatSessionMessageBaseAdapter(Context context,
            ArrayList<ChatMessage> messageArray) {
        this.messageArray = messageArray;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return messageArray.size();
    }

    @Override
    public Object getItem(int position) {
        return messageArray.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        ChatMessage chatMessage = messageArray.get(position);
        if (chatMessage.isSystemMsg()) {
            return 2;
        }
        return chatMessage.getUserinfo().getUsername()
                .equals(Toolbox.getMyInfo().getUsername()) ? 0 : 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();

        final ChatMessage chatMessage = messageArray.get(position);
        final UserInfo userInfo = chatMessage.getUserinfo();

        if (convertView == null) {
            if (chatMessage.isSystemMsg()) {
                convertView = mInflater.inflate(
                        R.layout.chat_message_row_system, null);
                viewHolder.message = (TextView) convertView
                        .findViewById(R.id.system_message_text_view);
                viewHolder.sender = null;
            } else {
                if (userInfo.getUsername().equals(
                        Toolbox.getMyInfo().getUsername())) {
                    convertView = mInflater.inflate(
                            R.layout.chat_message_row_me, null);
                } else {
                    convertView = mInflater.inflate(
                            R.layout.chat_message_row_other, null);
                }
                viewHolder.message = (TextView) convertView
                        .findViewById(R.id.message_text_view);

                // Choose a chat box
                if (userInfo.getUsername().equals(
                        Toolbox.getMyInfo().getUsername())) {
                    // Well... what color do I like?
                    if (bgColorResId == 0) {
                        bgColorResId = ColorPreference
                                .getMyBackgroundDrawableResId(Toolbox
                                        .getMyInfo()
                                        .getMessageBackgroundColor());
                    }
                    viewHolder.message.setBackgroundResource(bgColorResId);

                } else {
                    // Well... what color do others like?
                    viewHolder.message
                            .setBackgroundResource(ColorPreference
                                    .getOthersBackgroundDrawableResId(chatMessage
                                            .getUserinfo()
                                            .getMessageBackgroundColor()));
                }

            }
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        convertView.setTag(viewHolder);

        if (chatMessage.isSystemMsg()) {
            viewHolder.message.setText(chatMessage.getMessageContent());
        } else {
            viewHolder.message.setText(Html.fromHtml("<b>"
                    + userInfo.getNickName() + ": " + "</b>"
                    + chatMessage.getMessageContent()));
            viewHolder.sender = (ImageView) convertView
                    .findViewById(R.id.message_sender);
            viewHolder.sender.setImageDrawable(userInfo.getProfilePicture());
        }
        return convertView;
    }

    static class ViewHolder {
        public TextView message;
        public ImageView sender;
    }
}
